from bs4.element import Tag, NavigableString
from bs4 import BeautifulSoup
import numpy as np
import re

text_tag = ['a','span','em','font','a','h1','h2','h3','h4','h5','h6','strong','p','i','img']

def cal_parent(node_a, node_b):
    if id(node_a) == id(node_b):
        return node_a
    else:
        return cal_parent(node_a.parent, node_b.parent)

def sim(treeA, treeB, p=2, printable=False):
    dic_a = {'TextNode': 0}
    dic_b = {'TextNode': 0}
    dic_a['length'] = len(list(treeA.children))
    dic_a['length'] = len(list(treeB.children))
    count_node(treeA, dic_a)
    count_node(treeB, dic_b)

    sum = 0
    suma = 0
    sumb = 0
    for k in dic_a.keys():
        suma += dic_a[k]**2
        if dic_b.get(k):
            sum += dic_a[k]*dic_b[k]
    for k in dic_b.keys():
        sumb += dic_b[k]**2
    if printable:
        print('=============')
        print('dic_a:', dic_a)
        print('-------------')
        print('dic_b:', dic_b)
        print('-------------')
        print('sum:', sum)
        print('suma:', suma)
        print('sumb:', sumb)
    if (suma == 0)|(sumb == 0):
        return 0
    return sum/((suma**0.5)*(sumb**0.5))

def count_node(tree, dic):
    if not tree:
        return 0
    if isinstance(tree, Tag):
        t = tree.name
        classes = tree.attrs.get('class')
        if t not in text_tag:    
            if not dic.get(t):
                dic[t] = 1
            else:
                dic[t] += 1
        if classes:
            for c in classes:
                if not dic.get(c):
                    dic[c] = 1
                else:
                    dic[c] += 1
        for n in tree.children:
            count_node(n, dic)
    else:
        dic['TextNode'] += 1

def simplify(node):
    if isinstance(node, Tag):
        text = str(node)
        reg = re.compile(r'<\s*\/?[p|span|a|font|em|i|br].*>');
        text = reg.sub('',text)
        return BeautifulSoup(text,'lxml').body
    else:
        return node

def is_auth(node):
    while node:
        if isinstance(node, NavigableString):
            node = node.parent
            continue
        if node.attrs.get('class'):
            for i in node.attrs.get('class'):
                if len(re.findall('auth|user',i)) > 0:
                    return True
        node = node.parent
    return False
